package utils;

import java.util.ArrayList;
import java.util.HashMap;

public class Room {
	
	int id;
	ArrayList<String[]> allDoors = new ArrayList<String[]>();; // [0] state; [1] position
	ArrayList<String[]> unexploredDoors = new ArrayList<String[]>(); // [0] state; [1] position
	HashMap<String, Integer> connections = new HashMap<String, Integer>();//so pode haver portas com state aberto! DUH
	
	public Room(int _id, ArrayList<String[]> doors){
		id = _id;
		//unexploredDoors = doors;
		for(int i=0; i<doors.size(); i++){
			unexploredDoors.add(doors.get(i));
			allDoors.add(doors.get(i));
		}
	}

	public int getId() {
		return id;
	}

	public ArrayList<String[]> getAllDoors() {
		return allDoors;
	}

	public ArrayList<String[]> getUnexploredDoors() {
		return unexploredDoors;
	}

	public HashMap<String, Integer> getConnections() {
		return connections;
	}
	
	
	public void UpdateDoorsState(String doorPos) {
		for(int i=0; i < unexploredDoors.size(); i++){
			if(unexploredDoors.get(i)[1].equals(doorPos)){
				unexploredDoors.remove(i);
				String[] aux = new String[2];
				aux[0] = "1";
				aux[1] = doorPos;
				unexploredDoors.add(aux);
			}
		}
		
	}
	
	public void UpdateDoorsMap_andRemove(String doorPos, int newRoomId) {
		for(int i=0; i < unexploredDoors.size(); i++){
			if(unexploredDoors.get(i)[1].equals(doorPos)){
				
				unexploredDoors.remove(i);
				break;
			}
		}
		if(!connections.containsKey(doorPos))
			connections.put(doorPos, newRoomId);
		
	}
	
	
	public void setAllDoors(ArrayList<String[]> allDoors) {
		this.allDoors = allDoors;
	}

	public void setUnexploredDoors(ArrayList<String[]> unexploredDoors) {
		this.unexploredDoors = unexploredDoors;
	}

	public void setConnections(HashMap<String, Integer> connections) {
		this.connections = connections;
	}

	public String[] getClosestDoor(int x, int y, String state){
		String[] res = new String[2];
		int min=9999999;
		if(state.equals("open"))
			for(int i=0; i < unexploredDoors.size(); i++){
				String[] aux = unexploredDoors.get(i)[1].split("[*]");
				int xi = Integer.parseInt(aux[0]);
				int yi = Integer.parseInt(aux[1]);
				int minAux = (x-xi)*(x-xi)+(y-yi)*(y-yi);
				if(minAux<min && Integer.parseInt(unexploredDoors.get(i)[0]) == 1){
					min = minAux;
					res[0] = aux[0];
					res[1] = aux[1];
				}
			}
		else // closed
			for(int i=0; i < unexploredDoors.size(); i++){
				String[] aux = unexploredDoors.get(i)[1].split("[*]");
				int xi = Integer.parseInt(aux[0]);
				int yi = Integer.parseInt(aux[1]);
				int minAux = (x-xi)*(x-xi)+(y-yi)*(y-yi);
				if(minAux<min && Integer.parseInt(unexploredDoors.get(i)[0]) == 0){
					min = minAux;
					res[0] = aux[0];
					res[1] = aux[1];
				}
			}
		
		return res;
	}
	
	public int getDoorState(String pos){
		boolean contains=false;
		int state = -1;
		for(int i=0; i < unexploredDoors.size(); i++){
			if(unexploredDoors.get(i)[1].equals(pos)){
				contains=true;
				state = Integer.parseInt(unexploredDoors.get(i)[0]);
				break;
			}
		}
		if(!contains)
			return 1;//aberta
		else return state;
		
		
	}
	
	
	
	
	
}
